const express = require('express')
const router = express.Router()
const utils = require('../utils/utils')

router.post('/login',function (req,resp) {
    let {user,password} = req.body
    let sql = 'select * from user where username = ? and pwd = ?'
    utils.query(sql,[user,password],function (err,data) {
        if(!err){
            if(data.length>0){
                // 登录成功  保存session然后返回登陆成功的信息
                console.log( data[0])
                req.session.user = {
                    name:data[0].userName,
                    headImage:data[0].HeadImage
                }
                resp.send({code:200,message:"登录成功"});
            }else {
                // 登录失败  清空session 返回登录失败的信息
                resp.send({code:400,message:"登录失败"});
            }
        }else {
            // 返回数据库操作失败的信息
            resp.send({code:500,message:"数据库出错,请联系管理员"});
        }
    })
})

module.exports = router